<?php

namespace Api\Controller;
use Think\Controller;
use Com\Wechat;
use Com\WechatAuth;
use Think\Model;
class WechatController extends Controller {
    function handle() {
        /* 加载微信SDK */
        $wechat = new Wechat(C('WX_URL_TOKEN'));
        /* 获取请求信息 */
        $data = $wechat->request();
        if($data && is_array($data)){
            $USER = new Model('User');
            switch ($data['Event']) {
                case 'subscribe':
                    $WechatAutn = new WechatAuth(C('WX_APPID'), C('WX_SECRET'), getWxToken());
                    $uinfo = $WechatAutn->userInfo($data['FromUserName']);
                    #获取用户信息，并判断用户表是否有记录，有记录设置状态和关注时间
                    $ifExist = $USER->where(['openid'=>$uinfo['openid']])->find();
                    if($ifExist){
                        #存在 更新status 为1
                        $USER->where(['openid'=>$uinfo['openid']])->setField('status',1);
                        $content = C('WX_REWEL_STR');
                    } else {
                        $unameExist = $USER->where(['username'=>$uinfo['nickname']])->count();
                        if($unameExist > 0){
                            $username = $uinfo['nickname'].rand_string();
                        } else {
                            $username = $uinfo['nickname'];
                        }
                        #不存在，增加数据库记录
                        $data = [
                            'uuid' => makeUuid(),
                            'mobile' => '',
                            'username' => $username,
                            'password' => md5($uinfo['openid']),
                            'sex' => $uinfo['sex'],
                            'avatar' => $uinfo['headimgurl'],
                            'country' => $uinfo['country'],
                            'province' => $uinfo['province'],
                            'city' => $uinfo['city'],
                            'openid' => $uinfo['openid'],
                            'unionid' => isset($uinfo['unionid']) ? $uinfo['unionid'] : '',
                            'status' => 1,
                            'followtime' => time(),
                            'type' => 0,
                            'mid' => 0,
                        ];
                        $USER->add($data);
                        $content = C('WX_WEL_STR');
                    }
                    break;
                case "unsubscribe":#取关 设置status 为0
                    $USER->where(['openid'=>$data['FromUserName']])->setField('status',0);
                    break;
                default:
                    $content = "信息不能识别奥~";
                    break;  
            }
            $wechat->replyText($content);
            //在这里你可以分析用户发送过来的数据来决定需要做出什么样的回复
            //$content = ''; //回复内容，回复不同类型消息，内容的格式有所不同
            //$type    = ''; //回复消息的类型
            /* 响应当前请求(自动回复) */
            //$wechat->response($content, $type);
        }
    }
}
